﻿using FastEndpoints;
using TodoList.Data;

namespace TodoList.FastEndpoints.Features.Create;

internal sealed class Endpoint(ApplicationDbContext db) : Endpoint<Request, Response, Mapper>
{
    public override void Configure()
    {
        Post("/api/todoList");
        AllowAnonymous();
    }

    public override async Task HandleAsync(Request req, CancellationToken ct)
    {
        var entity = Map.ToEntity(req);
        await db.AddAsync(entity, ct);
        await db.SaveChangesAsync(ct);
        await SendAsync(new Response { Message = "Todo item created successfully." }, cancellation: ct);
    }
}
